package com.itboye.shop.category.store.orderfragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.itboye.shop.R;
import com.itboye.shop.base.BaseFragment;
import com.itboye.shop.bean.DingDanQuery;
import com.itboye.shop.category.adapter.GoodsOrderAdapter;
import com.itboye.shop.common.BasePtr;
import com.itboye.shop.net.ResultEntity;
import com.itboye.shop.presenter.ShopPresenter;
import com.itboye.shop.util.loadutil.Const;
import com.itboye.shop.util.loadutil.IsUtilUid;
import com.itboye.shop.util.loadutil.MAlert;
import com.itboye.shop.util.loadutil.SPUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by admin on 2017/2/8.
 */

@SuppressLint("ValidFragment")
public class GoodsOrderFragment extends BaseFragment implements Observer {

    private int type;
    ListView order_listView;
    GoodsOrderAdapter adapter;
    private LinearLayout linearOrder;

    ShopPresenter dingDanPresenter;
    String uid;
    DingDanQuery bean;
    List<DingDanQuery.ListEntity> beanList = new ArrayList<>();
    PtrFrameLayout commend_anchor_ptr;
    int pageIndex = 1;

    public GoodsOrderFragment(int type) {
        this.type = type;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_goodsorder,null);
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        uid = (String) SPUtils.get(getActivity(), null, Const.ID, "");
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新
        beanList=new ArrayList<>();
        dingDanPresenter = new ShopPresenter(this);
        showProgressDialog("数据加载中...", true);
        dingDanPresenter.orderQuery(IsUtilUid.sId(), uid, "", type + "", pageIndex + "", "10");
        adapter = new GoodsOrderAdapter(beanList, getActivity(), type);
        order_listView.setAdapter(adapter);
        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {//刷新的监听事件
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                pageIndex++;
                dingDanPresenter.orderQuery(IsUtilUid.sId(), uid, "", type + "", pageIndex + "", "10");
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                //  pageIndex = 1;
                dingDanPresenter.orderQuery(IsUtilUid.sId(), uid, "", type + "", pageIndex + "", "10");
            }
        });
         commend_anchor_ptr.autoRefresh(true);
    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.back:
                break;
        }

    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        commend_anchor_ptr.refreshComplete();
        if (entity != null) {
            if (entity.getEventType() == ShopPresenter.daingdan_query_success) {
                if (pageIndex==1) {
                    beanList.clear();
                }

                bean = (DingDanQuery) entity.getData();
                beanList.addAll(bean.getList());

                if (beanList.size() > 0) {
                    adapter.notifyDataSetChanged();
                    order_listView.setVisibility(View.VISIBLE);
                    linearOrder.setVisibility(View.GONE);
                } else {
                    order_listView.setVisibility(View.GONE);
                    linearOrder.setVisibility(View.VISIBLE);
                }
            }
            if (entity.getEventType() == ShopPresenter.daingdan_query_fail) {
                MAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
